/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-08-16 11:39:39
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-08-16 15:28:49
 * @FilePath: /vue3-uniapp/src/service/hook/useBeforeRouteLeave.ts
 */

import type { BeforeRouteLeaveHookState } from '@/plugins/uvuiTools';
import { getCurrentRoute } from '@/utils/route';

export const useBeforeRouteLeave = (
    fn: (state: BeforeRouteLeaveHookState) => void
) => {
    const route = getCurrentRoute();
    const subscription = uni.$uv.route.onBeforeRouteLeave((state) => {
        if (route === state.from) {
            fn(state);
        }
    });
    onBeforeUnmount(() => {
        subscription.unsubscribe();
        console.log(`[${from}]onBeforeRouteLeave订阅者释放`);
    });
};
